package com.smalls.redshoes.adapter;

import android.content.Context;
import android.graphics.Bitmap.Config;
import android.support.v4.internal.view.SupportMenu;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.smalls.newvideotwo.db.Record;
import com.smalls.redshoes.R;

import cn.bingoogolapple.androidcommon.adapter.BGAAdapterViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;

public class RSVodCategoryContentAdapter extends BGAAdapterViewAdapter<Record> {
    private static final String TAG = "RSVodCategoryContentAdapter";
    private Context context;
    private ImageLoader imageLoader;
    private ISShowDelete mIsShowDelete;
    private DisplayImageOptions options = new Builder().showImageOnLoading(R.drawable.rs_vod_item_bg).showImageForEmptyUri(R.drawable.rs_vod_item_bg).showImageOnFail(R.drawable.rs_vod_item_bg).cacheInMemory(false).cacheOnDisk(true).imageScaleType(ImageScaleType.EXACTLY).bitmapConfig(Config.RGB_565).displayer(new RoundedBitmapDisplayer(5)).build();

    public interface ISShowDelete {
        boolean isShowDelete();
    }

    public RSVodCategoryContentAdapter(Context context, ImageLoader imageLoader) {
        super(context, R.layout.rs_item_category_content);
        this.context = context;
        this.imageLoader = imageLoader;
    }

    protected void fillData(BGAViewHolderHelper viewHolderHelper, int position, Record model) {
        viewHolderHelper.getTextView(R.id.tv_name).setText(model.getTitle());
        setStringFormat(viewHolderHelper.getTextView(R.id.tv_delete_hint));
        setImageBitmap(viewHolderHelper, position);
        if (this.mIsShowDelete.isShowDelete()) {
            viewHolderHelper.getView(R.id.rl_delete).setVisibility(View.VISIBLE);
        } else {
            viewHolderHelper.getView(R.id.rl_delete).setVisibility(View.INVISIBLE);
        }
        if (TextUtils.isEmpty(model.getAlbumPic())) {
            viewHolderHelper.getImageView(R.id.iv_isNew).setVisibility(View.GONE);
            return;
        }
//        String dataStr = model.get();
//        LogUtil.d(TAG, "position:" + position + "  datastr=" + dataStr);
//        SimpleDateFormat  sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
//        sf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
//        try {
//            Date date = sf.parse(dataStr);
//            Date currentDate = new Date();
//            LogUtil.d(TAG, "position:" + position + "  currentDate=" + sf.format(currentDate));
//            int days = DateUtil.getBetweenDay(date, currentDate);
//            LogUtil.d(TAG, "position:" + position + "   days=" + days);
//            if (days >= 0 && days <= 7) {
//                viewHolderHelper.getImageView(R.id.iv_isNew).setVisibility(View.VISIBLE);
//            }
//        } catch (ParseException e) {
//            e.printStackTrace();
//            viewHolderHelper.getImageView(R.id.iv_isNew).setVisibility(View.GONE);
//        }
    }

    private void setStringFormat(TextView textView) {
        String text = this.context.getResources().getString(R.string.rs_vod_delete);
        int index = text.indexOf("OK");
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.setSpan(new ForegroundColorSpan(SupportMenu.CATEGORY_MASK), index, index + 2, 34);
        textView.setText(style);
    }

    private void setImageBitmap(BGAViewHolderHelper viewHolderHelper, int position) {
        ImageView iv_picture = viewHolderHelper.getImageView(R.id.iv_picture);
        String imgUrl = getImageUrl(position);
        if (TextUtils.isEmpty(imgUrl)) {
            iv_picture.setImageResource(R.drawable.rs_vod_item_bg);
            return;
        }
        String url = imgUrl;
        if (!url.equals(iv_picture.getTag())) {
            iv_picture.setTag(url);
            this.imageLoader.displayImage(url, iv_picture, this.options);
        }
    }

    private String getImageUrl(int position) {
        Record item = (Record) this.mData.get(position);
        String imgUrl = item.getAlbumPic();
        return imgUrl;
    }

    public void setOnIsShowDelete(ISShowDelete isShowDelete) {
        this.mIsShowDelete = isShowDelete;
    }
}
